var $j = jQuery.noConflict();
$j(function() {
	
	var totalCount = 0;
	var start = 0;
	var limit = 10;
	var pageCount = 0;
	var nowPage = 1;
	
	//加载国家数据
	function loadCountries(start, limit) {
		var url = "/MeowcashVisa/html/visa/FrontContinentAction_showContentCountries.action";
		var continentId = $j("#continentId").val();
		var data = {continentId:continentId, start:start, limit:limit};
		$j.post(url, data, function(data,status) {
			totalCount = data.total;
			$j("#dataTable").html('');
			for(var p = 0; p < data.invdata.length; p++) {
				var aa = '<tr><td>'
							+ '<table width="100%" style="background:url(/MeowcashVisa/HTML/images/line.gif) bottom center repeat-x">'
								+ '<tr>'
								  	+ '<td style="width:90px;text-align:center;" rowspan="2">'
								  		+ '<img src="' + data.invdata[p].nationalFlag + '" width=60 height=30 />'
								  	+ '</td>'
								  	+ '<td colspan="3"><h3 class="acls" style="margin:10 5px"><a target="_blank" href="/MeowcashVisa/html/visa/FrontContentAction_showContent.action?tableName=visaCountry&id=' +  data.invdata[p].id + '">' + data.invdata[p].nameCn + '</a></h3></td>'
								  	+ '<td width="110" style="width:102px;text-align:center;color:#f30;font-size:14px;font-weight:bold;">￥' + data.invdata[p].price + '</td>'
								+ '</tr>'
								+ '<tr>'
								  	+ '<td>签证类型:' + data.invdata[p].type + '</td><td>有效期:' + data.invdata[p].validityTime + '天</td><td>停留期:' + data.invdata[p].stayTime + '天</td>'
								  	+ '<td width="102"><a target="_blank" href="/MeowcashVisa/html/visa/FrontContentAction_showContent.action?tableName=visaCountry&id=' +  data.invdata[p].id + '">查看详情</a></td>'
								+ '</tr>'
							+ '</table>'
						+ '</td></tr>';
				$j("#dataTable").append(aa);
			}
			
			refreshPager();
			
		},'json');
	}
	
	function doPage(page) {
		if(page > pageCount)
			page = pageCount;
			
		if(page <= 0)
			page = 1;
			
		loadCountries((page - 1) * limit, limit);
		nowPage = page;
	}
	
	function doPrePage() {
		doPage(nowPage - 1);
	}
	
	function doNextPage() {
		doPage(nowPage + 1);
	}
	
	//刷新分页
	function refreshPager() {
		if(totalCount == 0) 
			return;
		
		pageCount = Math.floor(totalCount / limit) ;
		var mm = totalCount % limit;
		if(mm != 0) 
			pageCount++;
			
		$j("#pager").html('');
		
		var firstPage = '<a id="firstPage" style="cursor: pointer;">首页</a>&nbsp;&nbsp;';
		$j("#pager").append(firstPage);
		$j("#firstPage").bind("click", function() {
			doPage(1);
		});
		
		var prePage = '<a id="prePage" style="cursor: pointer;">上一页</a>&nbsp;&nbsp;';
		$j("#pager").append(prePage);
		$j("#prePage").bind("click", function() {
			doPrePage();
		});
		
		for(var p = 1; p <= pageCount; p++) {
			var page = '<a id="page_' + p + '" style="cursor: pointer;">' + p + '</a>&nbsp;&nbsp;';
			$j("#pager").append(page);
			$j("#page_" + p).bind("click", function() {
				doPage(this.text);
			});
		}
		
		var nextPage = '<a id="nextPage" style="cursor: pointer;">下一页</a>&nbsp;&nbsp;';
		$j("#pager").append(nextPage);
		$j("#nextPage").bind("click", function() {
			doNextPage();
		});
		
		var lastPage = '<a id="lastPage" style="cursor: pointer;">末页</a>';
		$j("#pager").append(lastPage);
		$j("#lastPage").bind("click", function() {
			doPage(pageCount);
		});
		
		$j("#pager").append('共 <strong>' + pageCount + '</strong>页, <strong>' + totalCount + '</strong>条记录');
	}
	
	//KnowledgeDiv
	function loadKnowledge() {
		var url = "/MeowcashVisa/html/visa/FrontKnowledgeAction_getKnowledge.action";
		$j.post(url, null, function(data,status) {
			$j("#knowledgeTable").html('');
			for(var p = 0; p < data.invdata.length; p++) {
				$j("#knowledgeTable").append(
					'<tr><td>' + '<a target="_blank" href="/MeowcashVisa/html/visa/FrontContentAction_showContent.action?tableName=Knowledge&id=' + data.invdata[p].id + '">' + data.invdata[p].title + '<a>' + '</td></tr>'
				)
			}
			
		},'json');
	}
	
	function loadNotice() {
		var url = "/MeowcashVisa/html/visa/FrontNoticeAction_getNotice.action";
		$j.post(url, null, function(data,status) {
			$j("#noticeTable").html('');
			for(var p = 0; p < data.invdata.length; p++) {
				$j("#noticeTable").append(
					'<tr><td>' + '<a target="_blank" href="/MeowcashVisa/html/visa/FrontContentAction_showContent.action?tableName=Notice&id=' + data.invdata[p].id + '">' + data.invdata[p].title + '<a>' + '</td></tr>'
				)
			}
			
		},'json');
	}
	
	loadKnowledge();
	loadNotice();
	loadCountries(0, limit);
	
});
